
import { useRequest } from 'alova'
import { Button, App } from 'antd'
import PropTypes from 'prop-types'
import { PlusOutlined, LoadingOutlined } from '@ant-design/icons'

import { Http } from '../Api'

const MapCategory = {
    'bb84e25e-1b42-44b5-bae8-7d8c7eb44814': { type: 'SceneMars3d', title: 'Cesium场景详情' },
    'a3d3ab74-3301-454d-becc-20cb15483295': { type: 'SceneAPS', title: 'Autodesk场景详情' }
}

// 操作按钮基础格式
const ButtonConfig = ({ type, danger, icon, title, click }) => {
    ButtonConfig.propTypes = {
        type: PropTypes.string,
        danger: PropTypes.bool,
        icon: PropTypes.string,
        title: PropTypes.string,
        click: PropTypes.func
    }
    const iconList = { add: <PlusOutlined />, resting: <LoadingOutlined /> }
    return (
        <Button type={type || 'link'} danger={danger} onClick={click} icon={iconList[icon] || ''}>
            {title || '操作'}
        </Button>
    )
}

// 基础操作按钮
export const BaseFormButton = ({ ProTableEvent, event, type, danger, icon, title, data }) => {
    BaseFormButton.propTypes = {
        ProTableEvent: PropTypes.object,
        event: PropTypes.string,
        type: PropTypes.string,
        danger: PropTypes.bool,
        icon: PropTypes.string,
        title: PropTypes.string,
        data: PropTypes.object,
    }

    const { notification } = App.useApp()
    const { send } = useRequest(data => Http(data), { immediate: false })

    const BaseFormOptions = {
        MapButton: () => {
            const { type, title } = MapCategory[data.category]
            ProTableEvent.emit({ type, title, data })
        },
        NavTo: () => { window.open(data.url) },
        NewAdd: () => ProTableEvent.emit({ type: 'FormOperations', title: '新增', data }),
        ThrAdd: () => ProTableEvent.emit({ type: data.type, title: data.title, data: data.data }),
        Modify: () => ProTableEvent.emit({ type: 'FormOperations', title: '修改', data }),
        Delete: () => ProTableEvent.emit({ type: 'DeleteModal', title: '删除', data }),
        OpenTwo: () => ProTableEvent.emit({ type: 'TabsA02', title, data }),
        OpenEdit: () => ProTableEvent.emit({ type: 'TabsEdit', title, data }),
        Annex: () => ProTableEvent.emit({ type: 'AttachmentPreview', title: '查看附件', data: data.url }),
        OpenModal: () => ProTableEvent.emit({ type: data.type, data: data.data }),
        MenuMap: () => ProTableEvent.emit({ type: 'MenuMapping', title: '菜单映射', data }),
        Resetting: () => window.location.reload(),
        Function: () => {
            const { tips, actionRef, url, path } = data
            send({ url, path }).then(() => {
                actionRef.current.reload()
                notification.success({ key: 'send', message: '操作成功', description: tips })
            }).catch(error => {
                console.log('error', error)
                // if (error.message === 'SyntaxError: Unexpected end of JSON input') {
                //     actionRef.current.reload()
                //     notification.success({ key: 'send', message: '操作成功', description: tips })
                // } else {
                //     notification.error({ key: 'send', message: '操作失败', description: error.message })
                // }
            })
        }
    }

    return (
        <ButtonConfig
            type={type}
            danger={danger}
            icon={icon}
            title={title}
            click={BaseFormOptions[event]}
        />
    )
}